package cn.tedu.hospitalgroupproject.server.content.service.impl;

import cn.tedu.hospitalgroupproject.server.common.ex.ServiceException;
import cn.tedu.hospitalgroupproject.server.common.pojo.vo.PageData;
import cn.tedu.hospitalgroupproject.server.common.web.ServiceCode;
import cn.tedu.hospitalgroupproject.server.content.dao.persist.repository.IDoctorRepository;
import cn.tedu.hospitalgroupproject.server.content.pojo.entity.Doctor;
import cn.tedu.hospitalgroupproject.server.content.pojo.param.DoctorAddNewParam;
import cn.tedu.hospitalgroupproject.server.content.pojo.param.DoctorUpdateInfoParam;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DoctorListItemVO;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DoctorStandardVO;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DoctorTitleAppLimitStandardVO;
import cn.tedu.hospitalgroupproject.server.content.service.IDoctorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;



@Service
@Slf4j
public class DoctorServiceImpl implements IDoctorService {


    @Value("${hospital-appointment.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;

    @Autowired
    private IDoctorRepository doctorRepository;

    public DoctorServiceImpl() {
        log.info("创建业务对象:DoctorServiceImpl");
    }

    @Override
    public void addNew(DoctorAddNewParam doctorAddNewParam) {
        log.debug("开始处理[新增医生]的业务,参数:{}",doctorAddNewParam);

        Doctor doctor=new Doctor();
        BeanUtils.copyProperties(doctorAddNewParam,doctor);

        doctorRepository.insert(doctor);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理[删除指定ID医生数据]的业务,医生ID:{}",id);
        DoctorStandardVO currentVO=doctorRepository.getStandardById(id);
        if(currentVO==null){
            String message="获取ID下医生数据失败,尝试访问的医生数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        doctorRepository.deleteById(id);
    }

    @Override
    public void updateInfoById(DoctorUpdateInfoParam doctorUpdateInfoParam) {
        log.debug("开始处理【修改医生信息】的业务，参数：{}", doctorUpdateInfoParam);

        Long id = doctorUpdateInfoParam.getId();
        DoctorStandardVO currentDoctor = doctorRepository.getStandardById(id);
        if (currentDoctor == null ) {
            String message = "修改医生信息失败，尝试修改的医生数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        Doctor doctor=new Doctor();
        BeanUtils.copyProperties(doctorUpdateInfoParam, doctor);

        int rows=doctorRepository.updateById(doctor);
        if (rows != 1) {
            String message = "修改医生信息失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    private void updateIsAdminById(Long id, Integer isAdmin) {
        log.debug("开始处理[更新医生管理员权限]的业务,ID:{}",id);
        DoctorStandardVO currentVO= doctorRepository.getStandardById(id);
        if(currentVO==null){
            String message="更新医生管理员权限,尝试更新的医生数据不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode. ERROR_NOT_FOUND,message);
        }

        if (currentVO.getIsAdmin() == isAdmin) {
            String message = ISADMIN_TEXT[isAdmin] + "更新医生管理员权限失败，医生权限已经处于此状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        Doctor doctor=new Doctor();
        doctor.setIsAdmin(isAdmin);
        doctor.setId(id);
        int rows=doctorRepository.updateById(doctor);
        if(rows!=1){
            String message="更新医生权限失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }

    @Override
    public void setIsAdmin(Long id) {
        log.debug("开始处理【开启管理员权限】的业务，参数：{}", id);
        updateIsAdminById(id, 1);
    }

    @Override
    public void setIsNotAdmin(Long id) {
        log.debug("开始处理【关闭管理员权限】的业务，参数：{}", id);
        updateIsAdminById(id, 0);
    }

    @Override
    public DoctorStandardVO getStandardById(Long id) {
       log.debug("开始处理【根据ID查询标签】业务，参数：{}", id);
       DoctorStandardVO currentVO= doctorRepository.getStandardById(id);
       if(currentVO==null){
           String message="获取ID下医生数据失败,尝试访问的医生数据不存在!";
           log.warn(message);
           throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
       }
       return currentVO;
    }

    @Override
    public PageData<DoctorListItemVO> list(Integer pageNum) {
        log.debug("开始处理【查询医生列表】业务，页码：{}", pageNum);
        PageData<DoctorListItemVO> pageData = doctorRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<DoctorListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【医生列表】业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageData<DoctorListItemVO> pageData = doctorRepository.list(pageNum, pageSize);
        return pageData;
    }
}
